package Market.exchange;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

public class ReadFromDirextory {

    public ArrayList<ArrayList<MarketData>> exchangeMarketData = new ArrayList<>();
    public ArrayList<MarketData> cflMarketData = new ArrayList<>();
    public ArrayList<MarketData> shlMarketData = new ArrayList<>();
    public ArrayList<MarketData> szlMarketData = new ArrayList<>();
    public void readFromDirectory(String dirPath) {
        File file = new File(dirPath);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                readFromDirectory(f.getAbsolutePath());
            }
        } else {
            String line;
            String[] colVal;
            String csvSplitBy = ",";
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                if ((line = br.readLine()) == null)
                    return;
                while ((line = br.readLine()) != null) {
                    line = line.replace("\"", "");
                    line = line.replace("NULL", "0");
                    colVal = line.split(csvSplitBy);
                    setValue(file.getName(),colVal);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public int getMaxSize(){
        int len = Math.max(cflMarketData.size(),Math.max(shlMarketData.size(), szlMarketData.size()));
        // exchangeMarketData.clear();
        // for(int i=0;i<len;i++){
        //     // if(i<cflMarketData.size())exchangeMarketData.add(cflMarketData.get(i));
        //     // if(i<shlMarketData.size())exchangeMarketData.add(shlMarketData.get(i));
        //     // if(i<szlMarketData.size())exchangeMarketData.add(szlMarketData.get(i));
        // }
        return len;
    }


    public void setValue_cfl2(String key, String[] colVals){
        MarketData marketData = new MarketData();
        marketData.setKey(key);
        marketData.setSecurityid(colVals[0]);
        marketData.setMarket(colVals[1]);
        marketData.setDate(colVals[2]);
        marketData.setTime(Double.valueOf(colVals[3]));
        marketData.setQuote_type(colVals[4]);
        marketData.setPreclose(Double.valueOf(colVals[5]));
        marketData.setOpen(Double.valueOf(colVals[6]));
        marketData.setHigh(Double.valueOf(colVals[7]));
        marketData.setLow(Double.valueOf(colVals[8]));
        marketData.setLast(Double.valueOf(colVals[9]));

        cflMarketData.add(marketData); 
    }
    public void setValue_shl2(String key, String[] colVals){
        MarketData marketData = new MarketData();
        marketData.setKey(key);
        marketData.setSecurityid(colVals[0]);
        marketData.setMarket(colVals[1]);
        marketData.setDate(colVals[2]);
        marketData.setTime(Double.valueOf(colVals[3]));
        marketData.setQuote_type(colVals[4]);
        marketData.setPreclose(Double.valueOf(colVals[6]));
        marketData.setOpen(Double.valueOf(colVals[7]));
        marketData.setHigh(Double.valueOf(colVals[8]));
        marketData.setLow(Double.valueOf(colVals[9]));
        marketData.setLast(Double.valueOf(colVals[10]));
        
        shlMarketData.add(marketData); 
    }
    public void setValue_szl2(String key, String[] colVals){
        MarketData marketData = new MarketData();
        marketData.setKey(key);
        marketData.setSecurityid(colVals[0]);
        marketData.setMarket(colVals[1]);
        marketData.setDate(colVals[2]);
        marketData.setTime(Double.valueOf(colVals[3]));
        marketData.setQuote_type(colVals[4]);
        marketData.setPreclose(Double.valueOf(colVals[6]));
        marketData.setOpen(Double.valueOf(colVals[7]));
        marketData.setHigh(Double.valueOf(colVals[8]));
        marketData.setLow(Double.valueOf(colVals[9]));
        marketData.setLast(Double.valueOf(colVals[10]));
        
        szlMarketData.add(marketData);  
    }

    public void setValue(String key, String[] colVals){
        if(key.contains("cfl")){
            setValue_cfl2("cfl", colVals);
        }
        else if(key.contains("szl")){
            setValue_szl2("szl", colVals);
        }
        else if(key.contains("shl")){
            setValue_shl2("shl", colVals);
        }

        exchangeMarketData.add(cflMarketData);
        exchangeMarketData.add(szlMarketData);
        exchangeMarketData.add(shlMarketData);
    }

    public void setValue_clf2_all(String key,String[] colVals) {
        MarketData marketData = new MarketData();
        marketData.setKey(key);
        marketData.setSecurityid(colVals[0]);
        marketData.setMarket(colVals[1]);
        marketData.setDate(colVals[2]);
        marketData.setTime(Double.valueOf(colVals[3]));
        marketData.setQuote_type(colVals[4]);
        marketData.setPreclose(Double.valueOf(colVals[5]));
        marketData.setOpen(Double.valueOf(colVals[6]));
        marketData.setHigh(Double.valueOf(colVals[7]));
        marketData.setLow(Double.valueOf(colVals[8]));
        marketData.setLast(Double.valueOf(colVals[9]));
        // marketData.setClose(Double.valueOf(colVals[10]));
        // marketData.setTrading_phase_code(colVals[11]);
        // marketData.setOffer_prices(colVals[12]);
        // marketData.setOffer_volumes(colVals[13]);
        // marketData.setBid_prices(colVals[14]);
        // marketData.setBid_volumes(colVals[15]);
        // marketData.setTotal_volume_trade(Double.valueOf(colVals[16]));
        // marketData.setTotal_value_trade(Double.valueOf(colVals[17]));
        // marketData.setHigh_limited_price(Double.valueOf(colVals[18]));
        // marketData.setLow_limited_price(Double.valueOf(colVals[19]));
        // marketData.setTotal_position(Double.valueOf(colVals[20]));
        // marketData.setSettle_price(Double.valueOf(colVals[21]));
        // marketData.set_253(colVals[22]);
        // marketData.set_254(Double.valueOf(colVals[23]));
        // marketData.setPreopen_interest(Double.valueOf(colVals[24]));
        // marketData.setPresettle_price(Double.valueOf(colVals[25]));
        // marketData.setPredelta(Double.valueOf(colVals[26]));
        // marketData.setCurrdelta(Double.valueOf(colVals[27]));
        // marketData.setAction_day(colVals[28]);
        // exchangeMarketData.add(marketData);
    }

    public static void main(String[] args) {
        ReadFromDirextory readFromDirextory = new ReadFromDirextory();
        readFromDirextory.readFromDirectory("data/quotedata_csv");
        System.out.println(readFromDirextory.exchangeMarketData.size());
        System.out.println(readFromDirextory.exchangeMarketData.get(0).get(0).getRecordTime());
    }
}